/**
 * 
 */
package dms.repository.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;

import dms.model.role.DormSupervisor;
import dms.model.role.StudentAffairTeacher;
import dms.model.role.SystemAdministrator;
import dms.model.util.FindUtil;
import dms.model.util.PageIterator;
import dms.repository.SystemAdministratorRepository;
import dms.util.HibernateUtil;

/**
 * @author 李健
 *
 */
public class SystemAdministratorRepositoryHibernate implements SystemAdministratorRepository {
	
	private static Log log = LogFactory.getLog(SystemAdministratorRepositoryHibernate.class);

	@Override
	public void createDormSupervisor(DormSupervisor dormSupervisorInfo) {
		Session session = null;
		Transaction tx = null;
		
		try {
			 session = HibernateUtil.getSessionFactory().openSession();
			 tx = session.beginTransaction();
			 
			 session.save(dormSupervisorInfo);
			 
			 tx.commit();
			 
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public void createStduentAffairTeacher(
			StudentAffairTeacher studentAffairTeacherInfo) {
		Session session = null;
		Transaction tx = null;
		
		try {
			 session = HibernateUtil.getSessionFactory().openSession();
			 tx = session.beginTransaction();
			 
			 session.save(studentAffairTeacherInfo);
			 
			 tx.commit();
			 
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}
	

	@Override
	public void deleteDormSupervisorById(Long id) {
		Session session = null;
		Transaction tx = null;
		DormSupervisor ds = null;
		try {
			 session = HibernateUtil.getSessionFactory().openSession();
			 tx = session.beginTransaction();
			 
			 ds = (DormSupervisor) session.load(DormSupervisor.class, id);
			 session.delete(ds);
			 
			 tx.commit();
			 
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}
	

	@Override
	public void deleteStudentAffairTeacherById(Long id) {
		Session session = null;
		Transaction tx = null;
		StudentAffairTeacher sat = null;
		try {
			 session = HibernateUtil.getSessionFactory().openSession();
			 tx = session.beginTransaction();
			 
			 sat = (StudentAffairTeacher) session.load(StudentAffairTeacher.class, id);
			 session.delete(sat);
			 
			 tx.commit();
			 
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public PageIterator<DormSupervisor> findAllDormSupervisor(int start,
			int count) {
		Session session = null;
		Transaction tx = null;
		PageIterator<DormSupervisor> iter = null;
		List<DormSupervisor> list = null;
		Long allCount = null;
		
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			tx = session.beginTransaction();
			
			String countQuery = "select count(*) from DormSupervisor ";
			String findQuery = "select d from DormSupervisor d ";
			
			allCount = FindUtil.calculateAllCount(session, countQuery, null);
			list = FindUtil.getEntitiesByPage(session, findQuery, null, start, count);
			iter = new PageIterator<DormSupervisor>(list, allCount, start, count);
				
			tx.commit();
			
		} catch (Exception e) {
			log.error(e.toString());
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (session != null) {
				session.close();
			}
		}
		
		return iter;
	}

	@Override
	public PageIterator<StudentAffairTeacher> findAllStudentAffairTeacher(
			int start, int count) {
		Session session = null;
		Transaction tx = null;
		PageIterator<StudentAffairTeacher> iter = null;
		List<StudentAffairTeacher> list = null;
		Long allCount = null;
		
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			tx = session.beginTransaction();
			
			String countQuery = "select count(*) from StudentAffairTeacher ";
			String findQuery = "select s from StudentAffairTeacher s ";
			
			allCount = FindUtil.calculateAllCount(session, countQuery, null);
			list = FindUtil.getEntitiesByPage(session, findQuery, null, start, count);
			iter = new PageIterator<StudentAffairTeacher>(list, allCount, start, count);
				
			tx.commit();
			
		} catch (Exception e) {
			log.error(e.toString());
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (session != null) {
				session.close();
			}
		}
		
		return iter;
	}

	@Override
	public DormSupervisor findDormSupervisorById(Long id) {
		Session session = null;
		Transaction tx = null;
		DormSupervisor ds = null;
		try {
			 session = HibernateUtil.getSessionFactory().openSession();
			 tx = session.beginTransaction();
			 
			 ds = (DormSupervisor) session.load(DormSupervisor.class, id);
			//initialize property
			Hibernate.initialize(ds);
			 
			 tx.commit();
			 
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return ds;
	}

	@Override
	public StudentAffairTeacher findStudentAffairTeacherById(Long id) {
		Session session = null;
		Transaction tx = null;
		StudentAffairTeacher sat = null;
		try {
			 session = HibernateUtil.getSessionFactory().openSession();
			 tx = session.beginTransaction();
			 
			 sat = (StudentAffairTeacher) session.load(StudentAffairTeacher.class, id);
			//initialize property
			Hibernate.initialize(sat);
				
			 tx.commit();
			 
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return sat;
	}

	@Override
	public void updateDormSupervisor(DormSupervisor dormSupervisorInfo) {
		Session session = null;
		Transaction tx = null;
		DormSupervisor ds = null;
		try {
			 session = HibernateUtil.getSessionFactory().openSession();
			 tx = session.beginTransaction();
			 
			 ds = (DormSupervisor) session.merge(dormSupervisorInfo);
			 		 
			 tx.commit();
			 
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (session != null) {
				session.close();
			}
		}
		
	}

	@Override
	public void updateStduentAffairTeacher(
			StudentAffairTeacher studentAffairTeacherInfo) {
		Session session = null;
		Transaction tx = null;
		StudentAffairTeacher sat = null;
		try {
			 session = HibernateUtil.getSessionFactory().openSession();
			 tx = session.beginTransaction();
			 
			 sat = (StudentAffairTeacher) session.merge(studentAffairTeacherInfo);
					 
			 tx.commit();
			 
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (session != null) {
				session.close();
			}
		}		
		
	}

	@Override
	public SystemAdministrator findSystemAdministratorById(long id) {
		Session session = null;
		Transaction tx = null;
		SystemAdministrator sa = null;
		try {
			 session = HibernateUtil.getSessionFactory().getCurrentSession();
			 tx = session.beginTransaction();
			 
			 sa = (SystemAdministrator) session.load(SystemAdministrator.class, id);
					 
			 tx.commit();
			 
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (session != null) {
				if (session.isOpen()) {
					session.close();
				}
			}
		}	
		return sa;
	}

	@Override
	public void createSystemAdministrator(
			SystemAdministrator systemAdministrator) {
		
		Session session = null;
		Transaction tx = null;
		SystemAdministrator sa = null;
		try {
			 session = HibernateUtil.getSessionFactory().getCurrentSession();
			 tx = session.beginTransaction();
			 
			session.save(systemAdministrator);
					 
			 tx.commit();
			 
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (session != null) {
				if (session.isOpen()) {
					session.close();
				}
			}
		}	
		
	}

	@Override
	public void deleteSystemAdministratorById(long id) {
		Session session = null;
		Transaction tx = null;
		SystemAdministrator sa = null;
		try {
			 session = HibernateUtil.getSessionFactory().getCurrentSession();
			 tx = session.beginTransaction();
			 
			 sa = (SystemAdministrator) session.load(SystemAdministrator.class, id);
			 session.delete(sa);
					 
			 tx.commit();
			 
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (session != null) {
				if (session.isOpen()) {
					session.close();
				}
			}
		}	
		
	}

	@Override
	public PageIterator<SystemAdministrator> findAllSystemAdministrator(
			int start, int count) {
		
		Session session = null;
		Transaction tx = null;
		PageIterator<SystemAdministrator> iter = null;
		List<SystemAdministrator> list = null;
		Long allCount = null;
		String countQuery = " select count(*) from SystemAdministrator s ";
		String findQuery = " select s from SystemAdministrator s ";
		
		try {
			 session = HibernateUtil.getSessionFactory().getCurrentSession();
			 tx = session.beginTransaction();

			 allCount = FindUtil.calculateAllCount(session, countQuery, null);
			 list = FindUtil.getEntitiesByPage(session, findQuery, null, start, count);
			iter = new PageIterator<SystemAdministrator>(list, allCount, start, count);		 
			 
			 tx.commit();
			 
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (session != null) {
				if (session.isOpen()) {
					session.close();
				}
			}
		}
		
		return iter;
	}

	@Override
	public void updateSystemAdministrator(
			SystemAdministrator systemAdministrator) {
		
		Session session = null;
		Transaction tx = null;
		SystemAdministrator sa = null;
		try {
			 session = HibernateUtil.getSessionFactory().getCurrentSession();
			 tx = session.beginTransaction();
			 
			session.merge(systemAdministrator);
					 
			 tx.commit();
			 
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (session != null) {
				if (session.isOpen()) {
					session.close();
				}
			}
		}	
		
	}

}
